﻿using Api.AutoMapper.ArticleResolver;
using Api.AutoMapper.ArticleTypeResolver;
using Api.Common;
using Api.Model.Dto.ArticleDto;
using Api.Model.Dto.ArticleTypeDto;
using Api.Model.Dto.TagDto;
using Api.Model.Model;
using Api.StartUpConfig;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Security.Policy;
using System.Threading.Tasks;

namespace Api.AutoMapper
{
    public class ArticleProfile:Profile
    {
        public ArticleProfile()
        {
            CreateMap<Article, GetArticleListOutput>();
            CreateMap<Article, GetArticleOutput>()
                .ForMember(x=>x.Content,c=>c.MapFrom((u,i)=> { return File.ReadAllText(StaticInfo.WebRootPath + u.Url); }))
                .ForMember(x => x.TagIds, y => y.MapFrom(o => o.TagArticles.Select(x=>x.TagId)));
            CreateMap<CreateArticleInput, Article>()
                .ForMember(x => x.Url, c => c.MapFrom<CreateArticleUrlResolver>())
                .ForMember(x => x.TagArticles, c => c.MapFrom((x, y) =>
                {
                    var tagArticles = new List<TagArticle>();
                    foreach (var item in x.TagIds )
                    {
                        tagArticles.Add(new TagArticle { TagId = item });
                    }
                    return tagArticles;
                }));
            CreateMap<UpdateArticleInput, Article>()
                .ForMember(x => x.TagArticles, c => c.MapFrom((x, y) =>
                {
                    var tagArticles = new List<TagArticle>();
                    foreach (var item in x.TagIds)
                    {
                        tagArticles.Add(new TagArticle { TagId = item,ArticleId = y.Id });
                    }
                    return tagArticles;
                })); 

        }
    }
    public class ArticleTypeProfile:Profile
    {
        public ArticleTypeProfile()
        {
            CreateMap<CreateArticleTypeInput, ArticleType>()
                .ForMember(x => x.Image, c => c.MapFrom<ArticleTypeImageResolver>());
            CreateMap<ArticleType, GetArticleTypeOutput>()
                .ForMember(x=>x.ArticleCount, c=> c.MapFrom(o=>o.Articles.Count));
            CreateMap<UpdateArticleTypeInput, ArticleType>();
        }
    }
    public class TagProfile:Profile
    {
        public TagProfile()
        {
            CreateMap<CreateTagInput, Tag>();
            CreateMap<Tag, GetTagOutput>()
                .ForMember(x => x.ArticleCount, c => c.MapFrom(o => o.TagArticles.Count)); ;
            CreateMap<UpdateTagInput, Tag>();
        }
    }
}
